﻿@page "/posts/"
@page "/posts/page/{page:int}"
@page "/posts/{page:int}"

@if (posts == null)
{
    <Loading />
}
else
{
    <div class="post-wrap archive">
        @if (posts.Success && posts.Result.Item.Any())
        {
            @foreach (var item in posts.Result.Item)
            {
                <h3>@item.Year</h3>
                @foreach (var post in item.Posts)
                {
                    <article class="archive-item">
                        <NavLink href="@("/post"+post.Url)">@post.Title</NavLink>
                        <span class="archive-item-date">@post.CreationTime</span>
                    </article>
                }
            }
            <nav class="pagination">
                @for (int i = 1; i <= TotalPage; i++)
                {
                    var _page = i;

                    if (page == _page)
                    {
                        <span class="page-number current">@_page</span>
                    }
                    else
                    {
                        <a class="page-number" @onclick="@(() => RenderPage(_page))" href="/posts/@_page">@_page</a>
                    }
                }
            </nav>
        }
        else
        {
            <ErrorTip />
        }
    </div>
}

@code {
    /// <summary>
    /// 当前页码
    /// </summary>
    [Parameter]
    public int? page { get; set; }

    /// <summary>
    /// 限制条数
    /// </summary>
    private int Limit = 15;

    /// <summary>
    /// 总页码
    /// </summary>
    private int TotalPage;

    /// <summary>
    /// 文章列表数据
    /// </summary>
    private ServiceResult<PagedList<QueryPostDto>> posts;

    /// <summary>
    /// 初始化
    /// </summary>
    protected override async Task OnInitializedAsync()
    {
        await Common.SetTitleAsync("Posts");

        // 设置默认值
        page = page.HasValue ? page : 1;

        await RenderPage(page);
    }

    /// <summary>
    /// 初始化完成后执行
    /// </summary>
    /// <returns></returns>
    protected override async Task OnParametersSetAsync()
    {
        // 从头部菜单过来page是没有值的，所以将page设为1，并请求数据，默认正常翻页请求不执行
        if (!page.HasValue)
        {
            page = 1;
            await RenderPage(page);
        }
    }

    /// <summary>
    /// 点击页码重新渲染数据
    /// </summary>
    /// <param name="page"></param>
    /// <returns></returns>
    private async Task RenderPage(int? page)
    {
        // 获取数据
        posts = await Http.GetFromJsonAsync<ServiceResult<PagedList<QueryPostDto>>>($"/blog/posts?page={page}&limit={Limit}");

        // 计算总页码
        TotalPage = (int)Math.Ceiling((posts.Result.Total / (double)Limit));
    }
}